#include "pagepositionsystem.h"
#include <QList>

QPointF PagePositionSystem::calculatePosition(const QRectF& pageRect, Position position,
                                            const QPointF& customOffset)
{
    if (!isValidPosition(position)) {
        position = getDefaultPosition();
    }

    QPointF basePosition;

    switch (position) {
        case Position::TopLeft:
            basePosition = QPointF(
                pageRect.left() + PhysicalConstants::SIDE_MARGIN_MM,
                pageRect.top() + PhysicalConstants::TOP_MARGIN_MM
            );
            break;

        case Position::TopCenter:
            basePosition = QPointF(
                pageRect.center().x(),
                pageRect.top() + PhysicalConstants::TOP_MARGIN_MM
            );
            break;

        case Position::TopRight:
            basePosition = QPointF(
                pageRect.right() - PhysicalConstants::SIDE_MARGIN_MM,
                pageRect.top() + PhysicalConstants::TOP_MARGIN_MM
            );
            break;

        case Position::BottomLeft:
            basePosition = QPointF(
                pageRect.left() + PhysicalConstants::SIDE_MARGIN_MM,
                pageRect.bottom() - PhysicalConstants::BOTTOM_MARGIN_MM
            );
            break;

        case Position::BottomCenter:
            basePosition = QPointF(
                pageRect.center().x(),
                pageRect.bottom() - PhysicalConstants::BOTTOM_MARGIN_MM
            );
            break;

        case Position::BottomRight:
            basePosition = QPointF(
                pageRect.right() - PhysicalConstants::SIDE_MARGIN_MM,
                pageRect.bottom() - PhysicalConstants::BOTTOM_MARGIN_MM
            );
            break;

        case Position::Custom:
            // 自定义位置默认为页面中心
            basePosition = pageRect.center();
            break;

        default:
            basePosition = calculateCenterPosition(pageRect);
            break;
    }

    // 应用用户自定义偏移
    return basePosition + customOffset;
}

QPointF PagePositionSystem::calculateTextOrigin(const QPointF& basePosition,
                                               const QSizeF& textSize, Position position)
{
    QPointF textOrigin = basePosition;

    switch (position) {
        case Position::TopLeft:
        case Position::BottomLeft:
            // 左对齐：基线位置就是计算出的位置
            textOrigin = basePosition;
            break;

        case Position::TopCenter:
        case Position::BottomCenter:
        case Position::Custom:
            // 居中对齐：向左偏移文本宽度的一半
            textOrigin.setX(basePosition.x() - textSize.width() / 2.0);
            break;

        case Position::TopRight:
        case Position::BottomRight:
            // 右对齐：向左偏移文本的全部宽度
            textOrigin.setX(basePosition.x() - textSize.width());
            break;
    }

    // Y轴调整：基线通常比视觉中心略低
    textOrigin.setY(basePosition.y() + textSize.height() * 0.25);

    return textOrigin;
}

QString PagePositionSystem::getDisplayName(Position position)
{
    switch (position) {
        case Position::TopLeft:     return "顶部左侧";
        case Position::TopCenter:   return "顶部居中";
        case Position::TopRight:    return "顶部右侧";
        case Position::BottomLeft:  return "底部左侧";
        case Position::BottomCenter:return "底部居中";
        case Position::BottomRight: return "底部右侧";
        case Position::Custom:      return "自定义位置";
        default:                    return "未知位置";
    }
}

bool PagePositionSystem::isValidPosition(Position position)
{
    return position >= Position::TopLeft && position <= Position::Custom;
}

QList<PagePositionSystem::Position> PagePositionSystem::getPresetPositions()
{
    return {
        Position::TopLeft,
        Position::TopCenter,
        Position::TopRight,
        Position::BottomLeft,
        Position::BottomCenter,
        Position::BottomRight
    };
}

double PagePositionSystem::mmToPixels(double mm, double dpi)
{
    return mm * dpi / PhysicalConstants::MM_PER_INCH;
}

double PagePositionSystem::pixelsToMm(double pixels, double dpi)
{
    return pixels * PhysicalConstants::MM_PER_INCH / dpi;
}

PagePositionSystem::Position PagePositionSystem::getDefaultPosition()
{
    return Position::BottomCenter;
}

// 私有工具方法实现
QPointF PagePositionSystem::calculateTopPosition(const QRectF& pageRect, const QPointF& offset)
{
    return QPointF(pageRect.center().x(), pageRect.top() + PhysicalConstants::TOP_MARGIN_MM) + offset;
}

QPointF PagePositionSystem::calculateBottomPosition(const QRectF& pageRect, const QPointF& offset)
{
    return QPointF(pageRect.center().x(), pageRect.bottom() - PhysicalConstants::BOTTOM_MARGIN_MM) + offset;
}

QPointF PagePositionSystem::calculateLeftPosition(const QRectF& pageRect, const QPointF& offset)
{
    return QPointF(pageRect.left() + PhysicalConstants::SIDE_MARGIN_MM, pageRect.center().y()) + offset;
}

QPointF PagePositionSystem::calculateRightPosition(const QRectF& pageRect, const QPointF& offset)
{
    return QPointF(pageRect.right() - PhysicalConstants::SIDE_MARGIN_MM, pageRect.center().y()) + offset;
}

QPointF PagePositionSystem::calculateCenterPosition(const QRectF& pageRect)
{
    return pageRect.center();
}

